package com.mask.im.business.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.SysOperationLogEntity;
import com.mask.im.business.mapper.OperationLogMapper;
import com.mask.im.business.model.dto.OperationLogDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.service.SysOperationLogService;
import com.mask.im.business.common.util.BeanCopy;
import com.mask.im.business.common.util.PageUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysOperationLogServiceImpl extends ServiceImpl<OperationLogMapper, SysOperationLogEntity> implements SysOperationLogService {

    @Override
    public PageResult<OperationLogDTO> listOperationLogs(ConditionVO conditionVO) {
        Page<SysOperationLogEntity> page = new Page<>(PageUtil.getLimitPage(), PageUtil.getSize());

        Page<SysOperationLogEntity> operationLogPage = this.page(page, new LambdaQueryWrapper<SysOperationLogEntity>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), SysOperationLogEntity::getOptModule, conditionVO.getKeywords())
                .or()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), SysOperationLogEntity::getOptDesc, conditionVO.getKeywords())
                .orderByDesc(SysOperationLogEntity::getId));

        List<OperationLogDTO> operationLogDTOs = BeanCopy.listCopy(operationLogPage.getRecords(), OperationLogDTO.class);
        return PageResult.<OperationLogDTO>builder().data(operationLogDTOs).total((int) operationLogPage.getTotal()).build();
    }

}
